package robocup.component.actions;

import robocup.component.AbstractAgent;

/**
 * Init-Actions are used to instantiate an agent (player/coach/trainer) on the
 * server, which afterwards is controlled by this client.
 */
public class InitAction
    extends Action {

  /**
   * The agent-type, which should be created:<br>
   * <ul>
   * <li>FIELD_PLAYER [0]
   * <li>GOALIE [1]
   * <li>COACH [2]
   * <li>TRAINER [4]
   * </ul>
   * These constants are defined in AbstractAgent.
   */
  private int    type;

  /**
   * The name of the team. All agents of the same team must connect with the
   * same name.
   */
  private String teamName;

  /**
   * Constructor: Connecting an agent to the server
   * 
   * @param type
   *          TRAINER, GOALIE, PLAYER or COACH
   * @param teamName
   *          the name of the team
   */
  public InitAction(
      final int type,
      final String teamName) {

    this.type = type;
    this.teamName = teamName;
  }

  /**
   * Returns the actions string representation:
   * 
   * @return (init [&lt;teamname&gt;] (version 9.4.5) [(goalie)])<br>
   *         <br>
   * 
   * Teamname is not used for the trainer, because he doesn't belong to any
   * team. The goalie-syntax is only inserted for type GOALIE. Note that the
   * version refers to the server-version used. The given one is compatible with
   * all server versions higher or equal to 9.4.5.
   */
  @Override
  public String toString() {

    final StringBuffer buf = new StringBuffer();
    buf.append("(init ");
    if (this.type != AbstractAgent.TRAINER) {
      buf.append(this.teamName + " ");
    }
    buf.append("(version 12)");
    if (this.type == AbstractAgent.GOALIE) {
      buf.append(" (goalie)");
    }
    buf.append(")");
    System.out.println("SEND " + buf.toString());
    return buf.toString();
  }
}
